﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace EndNoteHelper
{
    public class DgvMethod
    {
        // 方法：交错设置各行背景色
        public static void SetRowBackColor(DataGridView dgv)
        {            
            DataGridViewCellStyle heightLightCellStyle = new DataGridViewCellStyle();
            DataGridViewCellStyle normalLightCellStyle = new DataGridViewCellStyle();
            heightLightCellStyle.BackColor = Color.LightGoldenrodYellow;
            normalLightCellStyle.BackColor = Color.White;

            if (dgv.Rows.Count != 0)
            {
                foreach (DataGridViewRow dgvLogRow in dgv.Rows)
                {
                    if (dgvLogRow.Index % 2 == 1)
                    {
                        dgvLogRow.DefaultCellStyle = heightLightCellStyle;
                    }
                    else
                    {
                        dgvLogRow.DefaultCellStyle = normalLightCellStyle;
                    }
                }
            }
        }


        // 方法：交错设置各行背景色
        public static void SetColumnBackColor(DataGridView dgv)
        {
            DataGridViewCellStyle heightLightCellStyle = new DataGridViewCellStyle();
            DataGridViewCellStyle normalLightCellStyle = new DataGridViewCellStyle();
            heightLightCellStyle.BackColor = Color.LightGoldenrodYellow;
            normalLightCellStyle.BackColor = Color.White;

            if (dgv.Columns.Count != 0)
            {
                foreach (DataGridViewColumn dcLog in dgv.Columns)
                {
                    if (dcLog.Index % 2 == 1)
                    {
                        dcLog.DefaultCellStyle = heightLightCellStyle;
                    }
                    else
                    {
                        dcLog.DefaultCellStyle = normalLightCellStyle;
                    }
                }
            }
        }


        // 方法：自动调整列宽
        /// <summary>
        /// 功能：自动调整列宽。<para/>
        /// <param name="dgv">
        /// 参数：要调整列宽的DataGridView对象。</param>
        /// </summary>
        public static void AutoResizeColumnWidth(DataGridView dgv)
        {
            // 调整行头列
            if (true == dgv.RowHeadersVisible)
            {
                dgv.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
            }
            

            // 自动调整列宽
            if (dgv.Columns.Count > 0)
            {
                for (int i = 0; i < dgv.Columns.Count; i++)
                {
                    dgv.AutoResizeColumn(i, DataGridViewAutoSizeColumnMode.AllCells);

                    if (dgv.Columns[i].Width < 50)
                    {
                        dgv.AutoResizeColumn(i, DataGridViewAutoSizeColumnMode.ColumnHeader);
                    }
                }
            }
        }


        // 方法：设置行背景色
        /// <summary>
        /// 功能： 设置行背景色。<para/>
        /// <param name="dgvRow">
        /// 参数1：要改变背景色的行。<para/></param>
        /// <param name="color">
        /// 参数2：要设置的颜色。Color类型的结构体。Empty等价于LightGoldenrodYellow</param></summary>
        public static void SetRowBackColor(DataGridViewRow dgvRow, Color color)
        {
            DataGridViewCellStyle heightLightCellStyle = new DataGridViewCellStyle();
            DataGridViewCellStyle normalLightCellStyle = new DataGridViewCellStyle();
            heightLightCellStyle.BackColor = Color.LightGoldenrodYellow;
            normalLightCellStyle.BackColor = Color.White;

            if (color != Color.Empty)
            {
                heightLightCellStyle.BackColor = color;
            }

            dgvRow.DefaultCellStyle = heightLightCellStyle;
        }

        public static void SetGroupBackColor(DataGridView dgv, int columnIndex, Color plainColor, Color highlightColor)
        {
            if (dgv.Rows.Count==0)
            {
                return;
            }

            SetRowBackColor(dgv.Rows[0], plainColor);

            if (dgv.Rows.Count==1)
            {
                return;
            }

            string preValue = dgv.Rows[0].Cells[columnIndex].Value.ToString();
            Color preColor = plainColor;

            for (int i = 1; i < dgv.Rows.Count; i++)
            {
                if (dgv.Rows[i].Cells[columnIndex].Value.ToString()==preValue)
                {
                    SetRowBackColor(dgv.Rows[i], preColor);
                }
                else
                {
                    preColor = highlightColor;
                    preValue = dgv.Rows[i].Cells[columnIndex].Value.ToString();
                    SetRowBackColor(dgv.Rows[i], preColor);
                }
            }
        }
    }
}